
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppAbout;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppAboutRepository;
import me.zhengjie.service.AppAboutService;
import me.zhengjie.service.dto.AppAboutDto;
import me.zhengjie.service.dto.AppAboutQueryCriteria;
import me.zhengjie.service.mapstruct.AppAboutMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-08-10
**/
@Service
@RequiredArgsConstructor
public class AppAboutServiceImpl implements AppAboutService {

    private final AppAboutRepository appAboutRepository;
    private final AppAboutMapper appAboutMapper;

    @Override
    public Map<String,Object> queryAll(AppAboutQueryCriteria criteria, Pageable pageable){
        Page<AppAbout> page = appAboutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appAboutMapper::toDto));
    }

    @Override
    public List<AppAboutDto> queryAll(AppAboutQueryCriteria criteria){
        return appAboutMapper.toDto(appAboutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppAboutDto findById(Integer id) {
        AppAbout appAbout = appAboutRepository.findById(id).orElseGet(AppAbout::new);
        ValidationUtil.isNull(appAbout.getId(),"AppAbout","id",id);
        return appAboutMapper.toDto(appAbout);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppAboutDto create(AppAbout resources) {
        return appAboutMapper.toDto(appAboutRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppAbout resources) {
        AppAbout appAbout = appAboutRepository.findById(resources.getId()).orElseGet(AppAbout::new);
        ValidationUtil.isNull( appAbout.getId(),"AppAbout","id",resources.getId());
        appAbout.copy(resources);
        appAboutRepository.save(appAbout);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            appAboutRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AppAboutDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppAboutDto appAbout : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类", appAbout.getCate());
            map.put("标题", appAbout.getTitle());
            map.put("图片", appAbout.getImgs());
            map.put("内容", appAbout.getContent());
            map.put("点击数", appAbout.getClick());
            map.put("状态", appAbout.getStatus());
            map.put(" createTime",  appAbout.getCreateTime());
            map.put(" updateTime",  appAbout.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}